#include "zhongzhi.h"
#include "utils/deviceutils.h"
#include "QThread"
#include "utils/zhongzhimanager.h"

ZhongZhi::ZhongZhi(QWidget *widget):GamePersent(widget){

}

void ZhongZhi::initData(QList<RoleBean*> *roleBeans)
{
    this->roleBeans = roleBeans;
}

void ZhongZhi::harvest()
{
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    QRectF location = QRectF(0.2f,0.2,0.8f,0.8f);
    Scalar startScalar = Scalar(29, 209, 161);
    Scalar endScalar = Scalar(31, 255, 255);
    QRectF rectf;
    if(!ImgHelper::getGameMatchPoint(":/zhongzhi/icon/zhongzhi/daocaoren.png",startScalar, endScalar,Setting::getInstance()->getImgDiscern(), location, &rectf)){
        return;
    }
    //左上
    Win32Util::moveMouse(rectf.left() - 15, rectf.top() - 15);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    Win32Util::mouseLeftClick();
    DeviceUtils::sendLog(widget, "收割左上");
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->waitHarvest));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    clickDest(":/zhongzhi/icon/zhongzhi/zhongzhi_allpickup.png",Scalar(), Scalar(),Setting::getInstance()->getImgDiscern(),QRectF(0.2f, 0.5f, 0.8f,0.9f));
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;

    //右上
    if(!ImgHelper::getGameMatchPoint(":/zhongzhi/icon/zhongzhi/daocaoren.png", startScalar, endScalar, Setting::getInstance()->getImgDiscern(), location, &rectf)){
        return;
    }
    Win32Util::moveMouse(rectf.right() + 15,rectf.top() - 15);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    Win32Util::mouseLeftClick();
    DeviceUtils::sendLog(widget, "收割右上");
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->waitHarvest));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    clickDest(":/zhongzhi/icon/zhongzhi/zhongzhi_allpickup.png",Scalar(), Scalar(),Setting::getInstance()->getImgDiscern(),QRectF(0.2f, 0.5f, 0.8f,0.9f));
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;



    //左下
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->waitHarvest));
    if(!ImgHelper::getGameMatchPoint(":/zhongzhi/icon/zhongzhi/daocaoren.png",startScalar, endScalar, Setting::getInstance()->getImgDiscern(), location, &rectf)){
        return;
    }
    Win32Util::moveMouse(rectf.left() - 15, rectf.bottom() + 80);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    Win32Util::mouseLeftClick();
    DeviceUtils::sendLog(widget, "收割左下");
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->waitHarvest));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    clickDest(":/zhongzhi/icon/zhongzhi/zhongzhi_allpickup.png",Scalar(), Scalar(),Setting::getInstance()->getImgDiscern(),QRectF(0.2f, 0.5f, 0.8f,0.9f));
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;


    //右下
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->waitHarvest));
    if(!ImgHelper::getGameMatchPoint(":/zhongzhi/icon/zhongzhi/daocaoren.png", startScalar, endScalar, Setting::getInstance()->getImgDiscern(), location, &rectf)){
        return;
    }
    Win32Util::moveMouse(rectf.right() + 15, rectf.bottom() + 80);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    Win32Util::mouseLeftClick();
    DeviceUtils::sendLog(widget, "收割右下");
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->waitHarvest));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;
    clickDest(":/zhongzhi/icon/zhongzhi/zhongzhi_allpickup.png",Scalar(), Scalar(),Setting::getInstance()->getImgDiscern(),QRectF(0.2f, 0.5f, 0.8f,0.9f));
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)return;

}

void ZhongZhi::run()
{
    int i = 0;
    for(RoleBean *roleBean: *roleBeans){
        ZhongZhiBean *bean =roleBean->zhongzhi;
        if(bean == nullptr) continue;
        while(true){
            i++;
            DeviceUtils::sendLog(widget, QString("该角色第%1次种植").arg(i));
            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
            if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)break;
            Win32Util::keyboardClick(VK_MENU, 'V');
            clickNPC(":/zhongzhi/icon/zhongzhi/daocaoren.png",true);
            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
            if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)break;
            if(bean->type == "早产"){
                ctopTime = 5 * 60 * 1000; //5分钟
                DeviceUtils::sendLog(widget, "早产农作物");
                clickDest(":/zhongzhi/icon/zhongzhi/zaochan.png", Scalar(23,170,255),Scalar(24,172,255),Setting::getInstance()->getImgDiscern(),QRectF(0,0.2f, 0.5f,0.8f));
            }else if(bean->type == "晚产"){
                ctopTime = 1 * 60 * 60 * 1000; //一个小时
                DeviceUtils::sendLog(widget, "晚产农作物");
                clickDest(":/zhongzhi/icon/zhongzhi/wanchan.png", Scalar(23,170,255),Scalar(24,172,255),Setting::getInstance()->getImgDiscern(),QRectF(0,0.2f, 0.5f,0.8f));
            }
            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
            if(GameManager::getInstantce()->roleTask != ROLE_TASK_ZHONG_ZHI)break;
            QString cropImg = ZhongZhiManager::getInstance()->getCorpImg(bean->crop);
            if(cropImg.isEmpty() || cropImg.isNull()){
                DeviceUtils::sendLog(widget, "未找到该农作物");
                break;
            }
            DeviceUtils::sendLog(widget, QString("种植%1").arg(bean->crop));
            clickDest(cropImg, Scalar(23,170,255),Scalar(24,172,255),Setting::getInstance()->getImgDiscern(),QRectF(0,0.2f, 0.5f,0.8f));

            QThread::msleep(ctopTime);
            harvest();
        }
    }
    emit finish();
}


void ZhongZhi::callBack(QString str)
{
    DeviceUtils::sendLog(widget, str);
}

bool ZhongZhi::isCancelTask()
{
    return GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL;
}
